package solo;

/**
 * @author tongchen
 * @create 2023-06-20 21:35
 */
public class LazyMan {
    //懒汉式单例对象
    //创建单例对象
    //使用volatile修饰，禁止指令重排序
private static volatile LazyMan lazyMan;
//私有化构造方法
    private  LazyMan(){

    }
    //创建实例对象
    public static LazyMan getLazyMan(){
            //判断是否为空，为空则创建
            if(lazyMan==null) {
                synchronized (LazyMan.class){
                    //第二次在synchronized之下判断是否为空
                    if(lazyMan==null){
                        lazyMan = new LazyMan();
                    }
            }
        }
        return lazyMan;
    }
}
class Test{
    public static void main(String[] args) {
        for(int i=0;i<10;++i){
            //创建线程
             Thread thread =new Thread(()->{
                 System.out.println(LazyMan.getLazyMan());
                     });
             thread.start();
        }
    }
}
